<template>
  <el-container>
    <el-header style="height: 50px">
      <span>个人信息</span>
    </el-header>
    <el-main>
      <el-form
        label-position="right"
        label-width="80px"
        ref="userInfoForm"
        :rules="userInforules"
        :model="userInfoForm"
      >
        <el-form-item
          label="用户名:"
          prop="name"
        >
          <el-input
            v-model="userInfoForm.username"
            size="small"
            placeholder="你的用户名"
            clearable
            @keyup.enter.native='centerDialogVisible = true'
          ></el-input>
        </el-form-item>
        <el-form-item
          label="邮箱:"
          prop="email"
        >
          <el-input
            v-model="userInfoForm.email"
            size="small"
            :readonly="true"
          ></el-input>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer style="margin-left:auto;margin-right:auto;">
      <el-button
        type="primary"
        round
        @click="centerDialogVisible = true"
      >保存</el-button>
      <el-dialog
        title="身份验证"
        :visible.sync="centerDialogVisible"
        width="30%"
        center
      >
        <span style="display: flex;justify-content:space-around;">
          <el-input
            type="password"
            v-model="userInfoForm.password"
            placeholder="请输入密码"
            size="small"
            :show-password="true"
            style="width:250px;"
            @keyup.enter.native="submitForm('userInfoForm')"
          ></el-input>
          <el-button
            type="primary"
            size="small"
            round
            @click="submitForm('userInfoForm')"
          >确认</el-button>
        </span>
      </el-dialog>
    </el-footer>
  </el-container>
</template>

<script>

export default {
  name: 'PersonalSetting',
  data () {
    return {
      centerDialogVisible:false,
      userInfoForm: {
        username: null,
        email: null,
        password: null,
        newPassword: null,
        checkNewPassword: null
      },
      userInforules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
      }
    }
  },
  created: function () {
    this.$http.defaults.headers.common['token'] = sessionStorage.getItem("token")
    this.$http.get("/api/user")
      .then((response) => {
        // console.log(response.data.user);
        this.userInfoForm.username = response.data.user.username;
        this.userInfoForm.email = response.data.user.email;
      })
      .catch(function (error) {
        if (error.response) {
                // 请求已发出，但服务器响应的状态码不在 2xx 范围内
                console.log(error.response.data);
                console.log(error.response.status);
                console.log(error.response.headers);
              } else {
                // Something happened in setting up the request that triggered an Error
                console.log('Error', error.message);
              }
              console.log(error.config);
      })
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async valid  => {
        if (valid) {
          this.$http.put("/api/user",this.userInfoForm)
            .then((response) => {
              // console.log(response.data);
              // console.log(response.status);
              if(response.status == 200) {
                this.$message({
                  message: "用户名更改成功",
                  type: "success",
                  duration: 3000
                })
                sessionStorage.setItem("username",this.userInfoForm.username)
              }
              else {
                this.$message({
                  message: "用户名更改失败",
                  type: "error",
                  duration: 3000
                })
              }
              setTimeout("window.location.reload()",1000);
            })
            .catch(function (error) {
              if (error.response) {
                // 请求已发出，但服务器响应的状态码不在 2xx 范围内
                console.log(error.response.data);
                console.log(error.response.status);
                console.log(error.response.headers);
              } else {
                // Something happened in setting up the request that triggered an Error
                console.log('Error', error.message);
              }
              console.log(error.config);
            })
        }
      })
    },
    resetForm () {
      this.$refs.userInfoForm.resetFields()
    }
  },
}
</script>

<style lang="less" scoped>
.el-header {
  width: 100%;
  line-height: 50px;
  display: flex;
  justify-content: flex-start;
  font-size: 16px;
  color: #00a1d6;
  border-bottom: 1px solid #e1e2e5;
}
.el-form {
  float: left;
}
</style>
